package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.scity.beens.HdglBannerBeen;
import com.example.scity.beens.HdglBeen;
import com.example.scity.beens.HdglTypeBeen;
import com.example.scity.beens.MovieBeen;
import com.example.scity.beens.NewsBeen;
import com.example.scity.beens.ShjfBannerBeen;
import com.example.scity.beens.ShjfTypeBeen;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HdglActivity extends AppCompatActivity {
    View view;
    RadioGroup radioGroup;
    HdglTypeBeen hdglTypeBeen;
    HdglBeen hdglBeen;
    Banner hdglbanner;
    ListView listView;
    LinearLayout hdglbaominglinear,hdglpllinear;
    List<String> imglist=new ArrayList<>();
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    DisplayHdglList();
                    break;
                case 1:
                    handlerbanner();
                    break;
                case 2:
                   // DisplayHdglType();
                    break;
            }
        }
    };


    private void DisplayHdglList() {
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return hdglBeen.rows.size();
            }

            @Override
            public Object getItem(int i) {
                return hdglBeen.rows.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                view = LayoutInflater.from(HdglActivity.this).inflate(R.layout.item_hdgl, null);
                ImageView imageView;
                imageView = view.findViewById(R.id.item_hdgl_imgUrl);
                Glide.with(view).load(Configs.Host + hdglBeen.rows.get(i).imgUrl).into(imageView);
                TextView name;
                name = view.findViewById(R.id.item_hdgl_name);
                name.setText(hdglBeen.rows.get(i).name);
                TextView content;
                content = view.findViewById(R.id.item_hdgl_content);
                content.setText(Html.fromHtml(hdglBeen.rows.get(i).content));
                TextView publishTime;
                publishTime = view.findViewById(R.id.item_hdgl_publishTime);
                publishTime.setText("发布时间:"+hdglBeen.rows.get(i).publishTime);
                content.setText(Html.fromHtml(hdglBeen.rows.get(i).content));
                TextView likeNum;
                likeNum = view.findViewById(R.id.item_hdgl_likeNum);
                likeNum.setText("点赞数:"+hdglBeen.rows.get(i).likeNum);
                TextView categoryId;
                categoryId = view.findViewById(R.id.item_hdgl_categoryName);
                categoryId.setText("活动类别:"+hdglBeen.rows.get(i).categoryId);
                return view;
            }
        });
    }

    /*private void DisplayHdglType() {
        for(int i = 0 ;i<hdglTypeBeen.data.size();i++){
            switch (i){
                case 0:
                    ((RadioButton)view.findViewById(R.id.hdgl_sw_radioButton)).setText(hdglTypeBeen.data.get(i).name);
                    break;
                case 1:
                    ((RadioButton)view.findViewById(R.id.hdgl_sh_radioButton)).setText(hdglTypeBeen.data.get(i).name);
                    break;
                case 2:
                    ((RadioButton)view.findViewById(R.id.hdgl_qz_radioButton)).setText(hdglTypeBeen.data.get(i).name);
                    break;
                case 3:
                    ((RadioButton)view.findViewById(R.id.hdgl_xy_radioButton)).setText(hdglTypeBeen.data.get(i).name);
                    break;
                case 4:
                    ((RadioButton)view.findViewById(R.id.hdgl_hy_radioButton)).setText(hdglTypeBeen.data.get(i).name);
                    break;
                case 5:
                    ((RadioButton)view.findViewById(R.id.hdgl_xx_radioButton)).setText(hdglTypeBeen.data.get(i).name);
                    break;
                default:
                    return;
            }
        }
        radioGroup.check(R.id.hdgl_sw_radioButton);
    }
*/
    /*private void LoadHdgl(int type) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.HdglList).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                hdglBeen2 = new Gson().fromJson(response.body().string(), HdglBeen.class);
                handler.sendEmptyMessage(3);//加载成功
            }
        });
    }*/
    private void handlerbanner() {
        hdglbanner=findViewById(R.id.hdgl_banner);
        hdglbanner.setImages(imglist);
        hdglbanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object o, ImageView imageView) {
                Glide.with(context).load(o).into(imageView);
            }
        });
        hdglbanner.setDelayTime(2000);
        hdglbanner.start();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hdgl);
        hdglbanner=findViewById(R.id.hdgl_banner);
        listView=findViewById(R.id.hdgl_listview);
        loadhdglbanner();
        LoadHdgllist();
        loadhdglbaominglinear();
        loadhdglpllinear();
        //InitListView();
       // InitHdglRadioGroup();//初始化radiogroup
    }
    private void loadhdglbaominglinear() {
        hdglbaominglinear=findViewById(R.id.hdglbaominglinear);
        hdglbaominglinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(HdglActivity.this,HdglBaomingActivity.class));
            }
        });
    }

    private void loadhdglpllinear() {
        hdglpllinear=findViewById(R.id.hdglpllinear);
        hdglpllinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(HdglActivity.this,HdglPlActivity.class));
            }
        });
    }
   /* private void InitHdglRadioGroup() {
        radioGroup = view.findViewById(R.id.hdgl_radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(hdglTypeBeen==null)return;
                switch (checkedId){
                    case R.id.hdgl_sw_radioButton:
                        if(hdglTypeBeen.data.size()>=1){
                            LoadHdgl(hdglTypeBeen.data.get(0).id);
                        }
                        break;
                    case R.id.hdgl_sh_radioButton:
                        if(hdglTypeBeen.data.size()>=2){
                            LoadHdgl(hdglTypeBeen.data.get(1).id);
                        }
                        break;
                    case R.id.hdgl_qz_radioButton:
                        if(hdglTypeBeen.data.size()>=3){
                            LoadHdgl(hdglTypeBeen.data.get(2).id);
                        }
                        break;
                    case R.id.hdgl_xy_radioButton:
                        if(hdglTypeBeen.data.size()>=4){
                            LoadHdgl(hdglTypeBeen.data.get(3).id);
                        }
                        break;
                    case R.id.hdgl_hy_radioButton:
                        if(hdglTypeBeen.data.size()>=5){
                            LoadHdgl(hdglTypeBeen.data.get(4).id);
                        }
                        break;
                    case R.id.hdgl_xx_radioButton:
                        if(hdglTypeBeen.data.size()>=6){
                            LoadHdgl(hdglTypeBeen.data.get(5).id);
                        }
                        break;
                }
            }
        });
        //radioGroup.check(R.id.radioButton);
    }
*/
    private void LoadHdgllist() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Configs.Host + APIInterface.HdglList)
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                hdglBeen = new Gson().fromJson(response.body().string(),HdglBeen.class);
                handler.sendEmptyMessage(0);
            }
        });
    }

    private void loadhdglbanner() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host + APIInterface.HDGLBannerList).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                HdglBannerBeen json = new Gson().fromJson(response.body().string(), HdglBannerBeen.class);
                for (int i = 0; i < json.rows.size(); i++) {
                    imglist.add(Configs.Host + json.rows.get(i).advImg);
                }
                handler.sendEmptyMessage(1);
            }
        });
    }
}